package com.guigu.dealservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.R;
import com.guigu.dealservice.dto.Equipment;
import com.guigu.dealservice.dto.User;
import com.guigu.dealservice.entity.BargainIntention;
import com.guigu.dealservice.feign.MyEquipmentFeign;
import com.guigu.dealservice.feign.MyUserFeign;
import com.guigu.dealservice.mapper.BargainIntentionMapper;
import com.guigu.dealservice.service.BargainIntentionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author janecraft
 * @since 2025-10-17
 */
@Service
public class BargainIntentionServiceImpl extends ServiceImpl<BargainIntentionMapper, BargainIntention> implements BargainIntentionService {
    @Autowired
    private MyUserFeign myUserFeign;
    @Autowired
    private MyEquipmentFeign myEquipmentFeign;
    @Override
    public R bargainIntentionPageList(Integer id, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<BargainIntention> bargainIntentionQueryWrapper = new QueryWrapper<>();
        bargainIntentionQueryWrapper.eq("equipment_id",id);
        List<BargainIntention> list = this.list(bargainIntentionQueryWrapper);

        for (BargainIntention bargainIntention:list){

            //远程调用-根据用户id查询返回用户名称
            User user = myUserFeign.queryUserById(Math.toIntExact(bargainIntention.getUserId()));
            if (user!=null){
                bargainIntention.setUserName(user.getUserName());
            }
            Equipment equipment = myEquipmentFeign.selectEquipmentByIdFeign(bargainIntention.getEquipmentId());
            if (equipment!=null){
                bargainIntention.setEquipment(equipment);
            }
        }
        PageInfo<BargainIntention> pageInfo = new PageInfo<>(list);
        return R.ok().data("pageInfo",pageInfo);
    }
}
